package cn.paraview.mfa.http;

import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;

import com.paraview.base.jar.core.tools.DateUnit;

import cn.paraview.mfa.AppContext;
import cn.paraview.mfa.Utils;
import cn.paraview.mfa.callback.RequestCallback;
import cn.paraview.mfa.constant.HttpConstant;

public class HttpManage {

	public static void getOtpRequest(RequestCallback requestCallback){
		Map<String, Object> params = new HashMap<String, Object>();
		params.put(HttpParameter.DECICE_ID, Utils.deviceId);
		Calendar calendar = Calendar.getInstance();
		String currentDate = DateUnit.format(calendar.getTime(),
				DateUnit.FORMAT_DATE_YYYY_MM_DD_HH_MM_SS);
		params.put(HttpParameter.TIME, currentDate);
		params.put(HttpParameter.PLATFORM, HttpParameter.ANDROID);
		String url = HttpConstant.GET_OTP;
		new HttpPostTask(url, params, null, null, requestCallback, HttpId.GET_OTP_ID).execute();	
	}

	public static void getOTPStatusRequest(RequestCallback requestCallback){
		Map<String, Object> params = new HashMap<String, Object>();
		params.put(HttpParameter.UID, AppContext.getInstance().getLastLoginUid());
		params.put(HttpParameter.DECICE_ID, Utils.deviceId);
		String url = HttpConstant.GET_OTP_STATUS;
		new HttpPostTask(url, params, null, null, requestCallback, HttpId.GET_OTP_STATUS_ID).execute();	
	}

	public static void loginRequest(String uid,String pwd,RequestCallback requestCallback){
		Map<String, Object> params = new HashMap<String, Object>();
		params.put(HttpParameter.UID, uid);
		params.put(HttpParameter.PASSWORD, pwd);
		params.put(HttpParameter.DECICE_ID, Utils.deviceId);
		params.put(HttpParameter.CLIENT_ID, HttpParameter.clientId);
		String url = HttpConstant.LOGIN;
		new HttpPostTask(url, params, null, null, requestCallback, HttpId.LOGIN_ID).execute();	
	}

	public static void logoutRequest(RequestCallback requestCallback){
		Map<String, Object> params = new HashMap<String, Object>();
		params.put(HttpParameter.DECICE_ID, Utils.deviceId);
		Map<String, String> headerParams = new HashMap<String, String>();
		headerParams.put(HttpParameter.ACCESS_TOKEN, Utils.token);
		headerParams.put(HttpParameter.USER_NAME, Utils.uid);
		String url = HttpConstant.LOGOUT;
		new HttpPostTask(url, params, null, headerParams, requestCallback, HttpId.LOGOUT_ID).execute();	
	}

	public static void getOTPTokenRequest(String uid,RequestCallback requestCallback){
		Map<String, Object> params = new HashMap<String, Object>();
		params.put(HttpParameter.CODE, Utils.code);
		params.put(HttpParameter.CLIENT_ID, HttpParameter.clientId);
		params.put(HttpParameter.CLIENT_SECRET, Utils.secretkey);
		params.put(HttpParameter.UID, uid);
		String url = HttpConstant.GET_OTP_TOKEN;
		new HttpPostTask(url, params, null, null, requestCallback, HttpId.GET_OTP_TOKEN_ID).execute();	
	}

	public static void LoginByScanRequest(String scanResult,RequestCallback requestCallback){
		Map<String, String> head = new HashMap<String, String>();
		head.put(HttpParameter.ACCESS_TOKEN, Utils.token);
		new HttpPostTask(scanResult, null, null, head, requestCallback, HttpId.LOGIN_BY_SCAN_ID).execute();	
	}

	public static void getRefreshTokenRequest(RequestCallback requestCallback){
		Map<String, Object> params = new HashMap<String, Object>();
		params.put(HttpParameter.CLIENT_ID, HttpParameter.clientId);
		params.put(HttpParameter.CLIENT_SECRET, Utils.secretkey);
		params.put(HttpParameter.REFRESH_TOKEN_OLD_ID, Utils.refresh_token);
		Map<String, String> head = new HashMap<String, String>();
		head.put(HttpParameter.ACCESS_TOKEN, Utils.token);
		String url = HttpConstant.GET_REFRESH_TOKEN;
		new HttpPostTask(url, params, null, head, requestCallback, HttpId.GET_REFRESH_TOKEN_ID).execute();	
	}
	
}
